<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>AngelScript: Fine tuning</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   
   </div>
   
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_finetuning.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Fine tuning </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p>Here's a few recommendations to get the most performance out of AngelScript.</p>
<h2><a class="anchor" id="doc_finetuning_1"></a>
Cache the functions and types</h2>
<p>Doing searches by function declaration or name is rather time consuming and should not be done more than once per function that will be called. The same goes for the types that you might use it.</p>
<p>Also try to use the actual <a class="el" href="classas_i_script_function.html">asIScriptFunction</a> or <a class="el" href="classas_i_object_type.html">asIObjectType</a> pointers instead of the ids where possible. This will save the engine from translating the id to the actual object.</p>
<p>You may use the user data in the various engine interfaces to store the cached information. For example, store a structure with the commonly used class methods as <a class="el" href="classas_i_object_type.html#a4d542ed1b449f83f712c38bf27f78821">user data</a> in the <a class="el" href="classas_i_object_type.html">asIObjectType</a> interface. This way you will have quick access to the functions when they need to be called.</p>
<h2><a class="anchor" id="doc_finetuning_2"></a>
Reuse the context object</h2>
<p>The context object is a heavy weight object and you should avoid allocating new instances for each call. The object has been designed to be reused for multiple calls.</p>
<p>Ideally the application will keep a simple memory pool of allocated context objects, where new objects are only allocated if the is no free objects in the pool.</p>
<div class="fragment"><pre class="fragment">std::vector&lt;asIScriptContext *&gt; pool;
<a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *GetContextFromPool()
{
  <span class="comment">// Get a context from the pool, or create a new</span>
  <a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *ctx = 0;
  <span class="keywordflow">if</span>( pool.size() )
  {
    ctx = *pool.rbegin();
    pool.pop_back();
  }
  <span class="keywordflow">else</span>
    ctx = engine-&gt;<a class="code" href="classas_i_script_engine.html#a2630e1cd03ffab0fee9b820bf0afe42a" title="Creates a new script context.">CreateContext</a>();

  <span class="keywordflow">return</span> ctx;
}

<span class="keywordtype">void</span> ReturnContextToPool(<a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *ctx)
{
  pool.push_back(ctx);
  
  <span class="comment">// Unprepare the context to free non-reusable resources</span>
  ctx-&gt;<a class="code" href="classas_i_script_context.html#ae3c18a2cc66c56f840e6ee4310287f65" title="Frees resources held by the context.">Unprepare</a>();
}
</pre></div><h2><a class="anchor" id="doc_finetuning_3"></a>
Compile scripts without line cues</h2>
<p>The line cues are normally placed in the bytecode between each script statement. These are where the VM will allow the execution to be suspended, and also where the line callback is invoked.</p>
<p>If you do not use the line callback then you may get a little more performance out of the script by compiling without the line cues.</p>
<div class="fragment"><pre class="fragment">engine-&gt;<a class="code" href="classas_i_script_engine.html#a1bce4e5f573a2ca0ff55163e28f761dd" title="Dynamically change some engine properties.">SetEngineProperty</a>(<a class="code" href="angelscript_8h.html#a53c2e8a74ade77c928316396394ebe0fa73b396e4ea6376f0962d19add962bd91" title="Remove SUSPEND instructions between each statement. Default: false.">asEP_BUILD_WITHOUT_LINE_CUES</a>);
</pre></div><h2><a class="anchor" id="doc_finetuning_4"></a>
Disable thread safety</h2>
<p>If your application only uses one thread to invoke the script engine, then it may be worth it to compile the library without the thread safety to gain a little more performance.</p>
<p>To do this, define the AS_NO_THREADS flag in the as_config.h header or in the project settings. </p>
</div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>

    <li class="footer">Generated on Sun Jan 29 2012 15:41:04 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
